package com.neoCalf.neoGroup.controller;

/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/19 15:29
 */

import com.neoCalf.neoCommon.entity.GroupInfo;
import com.neoCalf.neoCommon.enums.GroupStatusEnum;
import com.neoCalf.neoCommon.enums.JoinTypeEnum;
import com.neoCalf.neoCommon.enums.ResultCode;
import com.neoCalf.neoCommon.exception.BusinessException;
import com.neoCalf.neoCommon.model.R;
import com.neoCalf.neoCommon.model.dto.GroupInfoDto;
import com.neoCalf.neoCommon.utils.DataTool;
import com.neoCalf.neoGroup.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/group")
@Validated
public class GroupController {
    @Autowired
    private GroupService groupService;

    @PostMapping("/myGroupCount")
    public R<Long> myGroupCount(@RequestParam("ownerId") @NotEmpty String ownerId) {
        Long count = groupService.myGroupCount(ownerId);
        return R.ok(count);
    }

    @GetMapping("/loadMyGroup")
    public R<List<GroupInfo>> loadMyGroup(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        List<GroupInfo> groupInfoList = groupService.loadMyGroup(userId);
        return R.ok(groupInfoList);
    }

    @GetMapping("groupDetail")
    public R<GroupInfo> getGroupInfo(@NotEmpty String groupId) {
        GroupInfo groupInfo = groupService.getGroupInfo(groupId);
        return R.ok(groupInfo);
    }

    @GetMapping("groupInfo4Chat")
    public R<GroupInfoDto> getGroupInfo4Chat(@NotEmpty String groupId) {
        GroupInfoDto groupInfoDto = groupService.getGroupInfo4Chat(groupId);
        return R.ok(groupInfoDto);
    }

    @PostMapping("/updateGroupInfo")
    public R<String> updateGroupInfo(HttpServletRequest request, @RequestBody @NotNull GroupInfo groupInfo) throws UnsupportedEncodingException {
        String userId = request.getHeader("userId");
        String nickName = URLDecoder.decode(request.getHeader("nickName"), "UTF-8");
        groupService.updateGroupInfo(userId, nickName, groupInfo);
        return R.ok();
    }

    @PostMapping("/createGroup")
    public R<String> createGroup(HttpServletRequest request,
                                 @NotEmpty String groupId,
                                 @NotEmpty String groupName,
                                 String groupNotice,
                                 @NotNull Integer joinType,
                                 @NotEmpty String url) {
        String userId = request.getHeader("userId");
        groupService.createGroup(userId, groupId, groupName, groupNotice, joinType, url);
        return R.ok();
    }

    @PostMapping("/takeGroupId")
    public R<String> takeGroupId() {
        return R.ok(DataTool.getGroupId());
    }
}
